import { createWebHistory, createRouter } from "vue-router";
import { message } from "ant-design-vue";
import { useAuthorizationStore } from "@/stores/authorization";

const history = createWebHistory();
const router = createRouter({
  history,
  routes: [
    {
      //登录页面
      path: "/login",
      name: "Login",
      component: () => import("@/views/login/Login.vue"),
      meta: {
        title: "登录",
        transition: "animate__zoomIn",
      },
    },
    {
      //登录页面
      path: "/register",
      name: "Register",
      component: () => import("@/views/register/Register.vue"),
      meta: {
        title: "注册",
        transition: "animate__zoomIn",
      },
    },
    {
      path: "/",
      name: "index",
      component: () => import("@/views/index/Index.vue"),
      meta: {
        title: "首页",
        transition: "animate__fadeInDownBig",
      },
    },
    {
      path: "/personal",
      name: "Personal",
      component: () => import("@/views/personal/Index.vue"),
      redirect: "/about",
      meta: {
        title: "个人中心",
        transition: "animate__fadeInDownBig",
      },
      children: [
        {
          path: "/about",
          name: "About",
          component: () => import("@/views/personal/about/About.vue"),
          meta: {
            title: "个人信息",
            transition: "animate__fadeInDownBig",
          },
        },
        {
          path: "/store",
          name: "Store",
          component: () => import("@/views/personal/store/Store.vue"),
          meta: {
            title: "我的店铺",
            transition: "animate__fadeInDownBig",
          },
        },
        {
          path: "/order",
          name: "Order",
          component: () => import("@/views/personal/order/Order.vue"),
          meta: {
            title: "我的订单",
            transition: "animate__fadeInDownBig",
          },
        },
        {
          path: "/myGoods",
          name: "MyGoods",
          component: () => import("@/views/personal/mygoods/Goods.vue"),
          meta: {
            title: "我的商品",
            transition: "animate__fadeInDownBig",
          },
        },
        {
          path: "/address",
          name: "Address",
          component: () => import("@/views/personal/address/Address.vue"),
          meta: {
            title: "收获地址",
            transition: "animate__fadeInDownBig",
          },
        },
        {
          path: "/collectgoods",
          name: "CollectGoods",
          component: () =>
            import("@/views/personal/collectgoods/CollectGoods.vue"),
          meta: {
            title: "收藏宝贝",
            transition: "animate__fadeInDownBig",
          },
        },
        {
          path: "/collectstores",
          name: "CollectStore",
          component: () =>
            import("@/views/personal/collectstores/CollectStore.vue"),
          meta: {
            title: "收藏店铺",
            transition: "animate__fadeInDownBig",
          },
        },
        {
          path: "/payed",
          name: "Payed",
          component: () => import("@/views/personal/payed/Payed.vue"),
          meta: {
            title: "买过店铺",
            transition: "animate__fadeInDownBig",
          },
        },
      ],
    },
    {
      path: "/detail",
      name: "Detail",
      component: () => import("@/views/detail/Detail.vue"),
      meta: {
        title: "详情页",
        transition: "animate__fadeInDownBig",
      },
    },
    {
      path: "/cart",
      name: "Cart",
      component: () => import("@/views/cart/Cart.vue"),
      meta: {
        title: "购物车",
        transition: "animate__fadeInDownBig",
      },
    },
    {
      path: "/submitorder",
      name: "SubmitOrder",
      component: () => import("@/views/submitorder/SubmitOrder.vue"),
      meta: {
        title: "提交订单",
        transition: "animate__fadeInDownBig",
      },
    },
    {
      path: "/search",
      name: "Search",
      component: () => import("@/views/search/Search.vue"),
      meta: {
        title: "商品搜索",
        transition: "animate__fadeInDownBig",
      },
    },
    {
      path: "/searchstore",
      name: "SearchStore",
      component: () => import("@/views/searchstore/SearchStore.vue"),
      meta: {
        title: "店铺搜索",
        transition: "animate__fadeInDownBig",
      },
    },
    {
      path: "/payfor",
      name: "PayFor",
      component: () => import("@/views/payfor/PayFor.vue"),
      meta: {
        title: "立即支付",
        transition: "animate__fadeInDownBig",
      },
    },
    {
      path: "/pay",
      name: "pay",
      component: () => import("@/views/pay/Success.vue"),
      meta: {
        title: "支付成功",
        transition: "animate__fadeInDownBig",
      },
    },
    {
      path: "/category",
      name: "Category",
      component: () => import("@/views/category/Category.vue"),
      meta: {
        title: "分类页面",
        transition: "animate__fadeInDownBig",
      },
    },
    {
      path: "/shop",
      name: "Shop",
      component: () => import("@/views/shop/Shop.vue"),
      meta: {
        title: "商家店铺",
        transition: "animate__fadeInDownBig",
      },
    },
    {
      path: "/perimeter",
      name: "Perimeter",
      component: () => import("@/views/perimeter/Index.vue"),
      meta: {
        title: "周边",
        transition: "animate__fadeInDownBig",
      },
    },
    {
      path: "/:pathMatch(.*)*",
      name: "NotFound",
      component: () => import("@/views/404/NotFound.vue"),
    },
  ],
});

// GOOD
router.beforeEach((to, from, next) => {
  const authorizationStore = useAuthorizationStore();
  const authorization = authorizationStore.authorization;

  if (
    to.path === "/register" ||
    to.path === "/detail" ||
    to.path === "/category" ||
    to.path === "/shop" ||
    to.path === "/search" ||
    to.path === "/searchstore" ||
    to.path === "/pay" ||
    to.path === "/"
  ) {
    next();
  } else {
    if (to.path === "/login") {
      if (authorization) {
        next("/");
      } else {
        next();
      }
    } else if (authorization) {
      next();
    } else {
      next("/login");
      message.warning("请先登录哦！");
    }
  }
});

export default router;
